package com.lujianfei.recyclerview.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.lujianfei.recyclerview.R;
import com.lujianfei.recyclerview.entity.ListItem;

import java.util.List;

public class VerticalViewPagerAdapter extends RecyclerView.Adapter<VerticalViewPagerAdapter.ViewHolder> {

    List<ListItem> listItemList;
    int[] color = new int[]{0xffff0000,0xff00ff00,0xff0000ff,0xffffff00,0xff00ffff};

    public VerticalViewPagerAdapter(List<ListItem> listItemList) {
        this.listItemList = listItemList;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.adapter_vertial_viewpager, parent, false);
        ViewHolder viewHolder = new ViewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        ListItem listItem = this.listItemList.get(position);
        holder.title.setText(listItem.getTitle());
        holder.summary.setText(listItem.getSummary());
        holder.contentView.setBackgroundColor(color[position % color.length]);
    }

    @Override
    public int getItemCount() {
        return this.listItemList.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        View contentView;
        TextView title;
        TextView summary;

        public ViewHolder(View view) {
            super(view);
            contentView = view;
            title = view.findViewById(R.id.title);
            summary = view.findViewById(R.id.summary);
        }
    }
}
